//
//  MoreViewController.m
//  GolfFriend
//
//  Created by Vescky on 14-2-9.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "MoreViewController.h"
#import "AppDelegate.h"

@interface MoreViewController ()<UITableViewDelegate,UITableViewDataSource> {
    NSMutableArray *muneList;
}

@end

@implementation MoreViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //[self initView];
    [self setTitle:@"更多"];
    [super customBackButton];
    [self.navigationController.navigationBar setHidden:NO];
    
    muneList = [[NSMutableArray alloc] init];
    NSArray *moreList = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"MoreView" ofType:@"plist"]];
    muneList = [[NSMutableArray alloc] initWithArray:moreList];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.navigationController.navigationBar setHidden:NO];
    if (ShowToolbarInWindow) {
        [APP_DELEGATE setToolbarHidden:NO];
        [self.navigationItem setHidesBackButton:YES animated:NO];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)menuAction:(id)sender {
    UIButton *btn = (UIButton*)sender;
    btn.backgroundColor = [UIColor clearColor];
    id obj = NSClassFromString([[[muneList objectAtIndex:(btn.tag / 100 - 1)] objectAtIndex:(btn.tag % 100 - 1)] objectForKey:@"controller"]);
    UIViewController *vc = [[obj alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)menuBtnTouchIn:(id)sender {
    UIButton *btn = (UIButton*)sender;
    btn.backgroundColor = GetColorWithRGB(200, 200, 200);
}

- (IBAction)menuBtnTouchOut:(id)sender {
    UIButton *btn = (UIButton*)sender;
    btn.backgroundColor = [UIColor clearColor];
}

- (void)initView {
    CGRect tbRect = tbMenu.frame;
    if (iPhone5) {
        tbRect.size.height = 354;
    }
    else {
        tbRect.size.height = 266;
    }
    tbMenu.frame = tbRect;
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [muneList count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[muneList objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"MoreViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(!cell){
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MoreViewCell" owner:self options:nil] objectAtIndex:0];
    }
    
    if([cell respondsToSelector:@selector(setDataInfo:)]){
        [cell performSelector:@selector(setDataInfo:) withObject:[[muneList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
    }
    if([cell respondsToSelector:@selector(setDelegate:)]){
        [cell performSelector:@selector(setDelegate:) withObject:self];
    }
    
    [cell setTag:indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    id obj = NSClassFromString([[[muneList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"controller"]);
    UIViewController *vc = [[obj alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    [tbMenu deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.f;
}

@end
